﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

public partial class Main_Inventory_PurchaseOrderApproval : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["mode"] == "edit")
        {
            PanelDisplay.Visible = false;
            paneledit.Visible = true;
            string po_id = Request.QueryString["key"].ToString();
            if (!IsPostBack)
            {
                bindDdlStatus();
                bindEditFields(po_id);
            }
        }
        else
        {
            PanelDisplay.Visible = true;
            paneledit.Visible = false;
        }
        if (!IsPostBack)
        {
            bindgrid();
           
        }
    }
    private void bindDdlStatus()
    {
        //ListItem lstitem1 = new ListItem();
        //lstitem1.Text = "Pending";
        //lstitem1.Value = "1";

        ListItem lstitem1 = new ListItem();
        lstitem1.Text = "Approved";
        lstitem1.Value = "1";

        ListItem lstitem2 = new ListItem();
        lstitem2.Text = "Canceled";
        lstitem2.Value = "2";

        DdlStatus.Items.Add(lstitem1);
        DdlStatus.Items.Add(lstitem2);
        //DdlStatus.Items.Add(lstitem3);
        DdlStatus.Items.Insert(0, "--Status--");
    }
    protected void lnkbtnEdit_Click(object sender, EventArgs e)
    {
        if (DdlStatus.SelectedIndex != 0)
        {
            try
            {
                int po_id = Convert.ToInt32(Request.QueryString["key"].ToString());
                string sqledit = "update ACC_POMAIN set status=" + Convert.ToInt32(DdlStatus.SelectedValue.ToString()) + ",ApprovalBy='" + Session["username"].ToString() + "',ApprovalDate='" + "' where id=" + po_id;
                lblMsg.Text = sqledit;
                DU.executesql(sqledit);
               // Response.Redirect("PurchaseOrderApproval.aspx?mode=show&mat_category=" + Request.QueryString["mat_category"].ToString() + "&page_id=" + page_id + "&msg=updated");
            }
            catch (Exception exc)
            {
                //lblMsg.Attributes.Add("class", "true");
                //lblMsg.Text = exc.Message.ToString();
                //return;
            }
        }
        else
        {
            //divmsg.Style.Add("background-color", "#dc143c");
            //LblErrorMsg.Style.Add("background-color", "#dc143c");
            //LblErrorMsg.Text = "Select a status first!";
            DdlStatus.Focus();
        }
    }
    protected void bindgrid()
    {
        string sql1 = "select * from ACC_POMAIN";
        DataTable DT = DU.getdata(sql1);
        DataTable Display = new DataTable();
        if (DT.Rows.Count > 0)
        {
            Display.Columns.Add("POID", typeof(System.String));
            Display.Columns.Add("PONo", typeof(System.String));
            Display.Columns.Add("cdate", typeof(System.String));
            Display.Columns.Add("cname", typeof(System.String));
            Display.Columns.Add("ApprovalBy", typeof(System.String));
            Display.Columns.Add("Status", typeof(System.String));
            Display.Columns.Add("ApprovalDate", typeof(System.String));
            for (int i = 0; i < DT.Rows.Count; i++)
            {
                string vouchermain="";
                string prefix = "";
                string vono = "";
                string suffix = "";

                DataRow DRow = Display.NewRow();
               // DRow["ID"] = DT.Rows[i]["ID"];
                DateTime po_date = Convert.ToDateTime(DT.Rows[i]["cdate"].ToString());
                DRow["cdate"] = string.Format("{0:dd-MMM-yyyy}", po_date);
                DRow["POID"] = DT.Rows[i]["POID"];
                DRow["cname"] = DT.Rows[i]["cname"];
                if (DT.Rows[i]["PoPrefix"].ToString() != string.Empty)
                {
                    prefix = DT.Rows[i]["PoPrefix"].ToString()+"/";
                }
                if (DT.Rows[i]["PoVoucherNo"].ToString() != string.Empty)
                {
                    vono = DT.Rows[i]["PoVoucherNo"].ToString();
                }
                if (DT.Rows[i]["PoSuffix"].ToString() != string.Empty)
                {
                    suffix = "/" + DT.Rows[i]["PoSuffix"].ToString();
                }
                vouchermain = prefix + vono + suffix;
                DRow["PONo"] = vouchermain;

                if (DT.Rows[i]["Status"].ToString().Equals("1"))
                {
                    DRow["Status"] = "Pending";

                }
                if (DT.Rows[i]["Status"].ToString().Equals("2"))
                {
                    DRow["Status"] = "Approved";
                    if (DT.Rows[i]["ApprovalDate"].ToString() != string.Empty)
                    {
                        DRow["ApprovalDate"] = string.Format("{0:dd-MMM-yyyy}", Convert.ToDateTime(DT.Rows[i]["ApprovalDate"].ToString()));
                    }

                    if (DT.Rows[i]["ApprovalBy"].ToString() != string.Empty)
                    {
                        DRow["ApprovalBy"] = DT.Rows[i]["ApprovalBy"];
                    }
                }
                else if (DT.Rows[i]["Status"].ToString().Equals("3"))
                {
                    DRow["Status"] = "Canceled";
                }
                Display.Rows.Add(DRow);

              
            }
            grdDisplay.DataSource = Display;
            grdDisplay.DataBind();
        }  
    }
    protected void custPager_PageChanged(object sender, CustomPageChangeArgs e)
    {
        grdDisplay.PageSize = e.CurrentPageSize;
        grdDisplay.PageIndex = e.CurrentPageNumber;
        bindgrid();
    }
    protected void grdDisplay_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string po_id = grdDisplay.DataKeys[e.Row.RowIndex].Value.ToString();
            string sql_select = "select Status from ACC_POMAIN where POID=" + Convert.ToInt32(po_id);
            DataTable DTStatus = DU.getdata(sql_select);
            if (DTStatus.Rows.Count > 0)
            {
                if (DTStatus.Rows[0]["Status"].ToString() == "1")
                {
                    e.Row.Cells[6].ForeColor = System.Drawing.Color.Olive;
                }
                if (DTStatus.Rows[0]["Status"].ToString() == "2")
                {
                    e.Row.Cells[3].FindControl("lnkbtnEdit").Visible = false;
                    e.Row.Cells[3].FindControl("imgapproved").Visible = true;
                    e.Row.Cells[6].ForeColor = System.Drawing.Color.Green;
                }
                else
                {
                    e.Row.Cells[3].FindControl("lnkbtnEdit").Visible = true;
                    e.Row.Cells[3].FindControl("imgapproved").Visible = false;
                }
                if (DTStatus.Rows[0]["Status"].ToString() == "3")
                {
                    e.Row.Cells[6].ForeColor = System.Drawing.Color.Red;
                }              
            }
        }
    }
    protected void grdDisplay_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {
            string key = (string)e.CommandArgument;
            //Response.Redirect("PurchaseOrderApproval.aspx?mode=edit&key=" + key);           
            
            GridViewRow grd=(GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
            int index = grd.RowIndex;
            Label lblpono = (Label)grdDisplay.Rows[index].FindControl("lblpono");
            if (lblpono.Text == "")
            {
                lblMsg.Attributes.Add("class", "true");
                lblMsg.Text = "You Have to Set Purchase Order before Approval.";
            }
            else
            {
                Response.Redirect("PurchaseOrderFinanceApprove.aspx?mode=edit&key=" + key);
            }
        }
    }
    protected void BtnClose_Click(object sender, EventArgs e)
    {
         Response.Redirect("PurchaseOrderApproval.aspx");
    }
    private void bindEditFields(string po_id)
    {
        
        string sql_select = "select POID,PoPrefix,PoSuffix,PoVoucherNo,status,cname from ACC_POMAIN where POID=" + po_id;
        DataTable DT= DU.getdata(sql_select);
        if (DT.Rows.Count > 0)
        {
            string vouchermain = "";
            string prefix = "";
            string vono = "";
            string suffix = "";

            if (DT.Rows[0]["PoPrefix"].ToString() != string.Empty)
            {
                prefix = DT.Rows[0]["PoPrefix"].ToString() + "/";
            }
            if (DT.Rows[0]["PoVoucherNo"].ToString() != string.Empty)
            {
                vono = DT.Rows[0]["PoVoucherNo"].ToString();
            }
            if (DT.Rows[0]["PoSuffix"].ToString() != string.Empty)
            {
                suffix = "/" + DT.Rows[0]["PoSuffix"].ToString();
            }
            vouchermain = prefix + vono + suffix;
            lblpurchaseorder.Text = vouchermain;
           // lblpurchaseorder.Text = DT.Rows[0]["POID"].ToString();
            DdlStatus.SelectedValue = DT.Rows[0]["Status"].ToString();
            lblorderplace.Text = DT.Rows[0]["cname"].ToString();
        }
    }
    protected void BtnUpdate_Click(object sender, EventArgs e)
    {
      
        if (DdlStatus.SelectedValue != "0")
        {
            SqlParameter[] parm ={                                 
                                  new SqlParameter("@ApprovalBy",Session["username"].ToString()),
                                  new SqlParameter("@POID",Request.QueryString["key"].ToString()),
                                  new SqlParameter("@Status",DdlStatus.SelectedValue)
                               };
            DU.executeprocedure("usp_update_ACC_POMAIN", parm);
            lblMsg.Attributes.Remove("class");
            lblMsg.Attributes.Add("class", "true");
            lblMsg.Text = "Purchase order status Update sucessfully...";
        }
    }
    protected void grdDisplay_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdDisplay.PageIndex = e.NewPageIndex;
        bindgrid();
    }
    protected void grdDisplay_RowCreated(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType != DataControlRowType.Pager)
        {
            if (e.Row.RowState == DataControlRowState.Alternate)
            {
                e.Row.Attributes.Add("onmouseover", "this.className='rowhover'");
                e.Row.Attributes.Add("onmouseout", "this.className='gridview_alternate'");
                //e.Row.Attributes.Add("onclick", "this.className='rowselected'");
            }
            else if (e.Row.RowState == DataControlRowState.Normal)
            {
                e.Row.Attributes.Add("onmouseover", "this.className='rowhover'");
                e.Row.Attributes.Add("onmouseout", "this.className='gridviewitem'");
                //e.Row.Attributes.Add("onclick", "this.className='rowselected'");
            }
        }

    }
}
